<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkModelTest')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

/** 
 *  This is base class to test PDK models using SimpleTest framework via SimpleTest plugin
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
 //NOTE: Althought we do this include here, you should also include lanceur_spip.php in your test case
include_spip('inc/tests');
class PdkModelTest extends SpipTest {
    /**
     *  Model wich will be unit tested
     */
    protected $model;
    
    /**
     *  PdkTestTable
     */
    protected $test_table;
    
    /**
     *  Test data for model wich is being tested
     *  Fields must be identical to all main table fields and join tables fields (names/aliases and number)
     */
    //protected $model_data;
    
    /**
     *  Array of additional PDK models.
     *  Add model to this array to automatically create, delete and clear it's tables
     *  Format: array([pdk_model_name1]=>PdkModel1,[pdk_model_name2]=>PdkModel2)
     */
    protected $pdk_models = array();
    
    public function __construct(pdk_Model $model) {
        $this->model = $model;
        
        include_spip('classes/simpletest/class.PdkTestTable');
        $this->test_table = new PdkTestTable();
        
        $this->test_table->CreateTables($this->model);
        
        foreach ($this->pdk_models as $pdk_model) {            
            $this->test_table->CreateTables($pdk_model);
        }
    }
    
    public function __destruct() { //return;
        $this->test_table->DropTables($this->model);
        foreach ($this->pdk_models as $pdk_model) {            
            $this->test_table->DropTables($pdk_model);
        }
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
        //Clear tables
        $this->test_table->ClearTables($this->model);
        foreach ($this->pdk_models as $pdk_model) {            
            $this->test_table->ClearTables($pdk_model);
        }
        // Populate tables:
        $this->test_table->PopulateTables($this->model);
        foreach ($this->pdk_models as $pdk_model) {            
            $this->test_table->PopulateTables($pdk_model);
        }
    }
    
   function testInsertOneRow_DataWithID() {        
        $data = $this->model->test_data[0]; //var_dump($data); exit;
        $data[$this->model->primary_key] = 999;
        $this->model->insert($data);        
        
        // Check main table
        $sel_data = sql_fetsel('*',$this->model->main_table, $this->model->primary_key.'=999');
        // Filter data, and select only main fields to assert main table
        foreach ($this->model->main_fields as $field=>$alias) {
            $data_check_sel[$alias] = $sel_data[$field];
            $data_check[$alias] = $data[$alias];
        }
        $this->assertEqual($data_check, $data_check_sel);
        
        // Check join tables
        foreach ($this->model->joins as $table_name=>$join_data) {
            
        }                                           
	}
    
    /**
     *  Test get_total method
     */
    function testGetTotal() {  //exit;       
        $check_total = count($this->model->test_data);
        $this->assertEqual($check_total, $this->model->get_total());        
    }
    
    /**
     * 
     */
    function testSelectOneRowByID() {
        $data_check = $this->model->form_data_from_array($this->model->test_data[0]);
        
        $data_sel = $this->model->form_data_from_array($this->model->select_by_id($data_check[$this->model->primary_key]));
        
        $this->assertEqual($data_check, $data_sel);
    }
}
?>